﻿@model Traviesuras.Models.clsDocumento_cab

@{
    Layout = null;
    ViewBag.Title = "Cotización";
}
<script src="~/Scripts/jquery-1.10.2.js"></script>
<script src="~/Scripts/jquery-ui.js"></script>
<script src="~/Scripts/traviesuras/ConfigDoc.js"></script>
@*<script src="~/Scripts/Telerik/jquery.min.js"></script>*@
<script src="~/Scripts/Telerik/kendo.web.min.js"></script>
<script src="~/Scripts/Telerik/console.js"></script>

<link href="~/Content/jquery-ui.css" rel="stylesheet" />
<link href="~/Content/style.css" rel="stylesheet" />

<link href="~/Content/Telerik/kendo.common.min.css" rel="stylesheet" />
<link href="~/Content/Telerik/kendo.default.min.css" rel="stylesheet" />
<link href="~/Content/Telerik/kendo.rtl.min.css" rel="stylesheet" />
<link href="~/Content/Telerik/styles/examples-offline.css" rel="stylesheet" />
    <button id="btnNuevo" onclick="limpiar()">Nuevo</button>

@using (Html.BeginForm("Index", "Doc", FormMethod.Post))
{
    @Html.AntiForgeryToken()
    @*@Html.ValidationSummary(true)*@
    <div id="pantalla" hidden="hidden">
 <fieldset>
        <legend>@ViewBag.Title</legend>
    <div id ="downtab">
        <h3>DATOS DOCUMENTO</h3>
        <div>
            <table style="font-size:small;height:auto;width:auto;">
                <tr>
                    <td>@Html.LabelFor(model => model.id_doc_cab)</td>
                    <td>@Html.EditorFor(model => model.id_doc_cab)<input type="submit" value="..." /></td>
                    <td>@*@Html.ValidationMessageFor(model => model.id_doc_cab)*@</td>
                    <td></td>
                    <td></td>
                    <td>@Html.LabelFor(model => model.cod_documento)</td>
                    <td>@Html.EditorFor(model => model.cod_documento)<input type="submit" value="..." /></td>
                    <td>@Html.ValidationMessageFor(model => model.cod_documento)</td>

                    <td>@Html.LabelFor(model => model.fecha)</td>
                    <td>@Html.EditorFor(model => model.fecha)</td>
                    <td>@Html.ValidationMessageFor(model => model.fecha)</td>
                </tr>
                <tr>
                    <td>@Html.LabelFor(model => model.descripcion)</td>
                    <td colspan="9">@Html.TextBoxFor(model => model.descripcion, new { style = "width:400px" })</td>
                    <td>@Html.ValidationMessageFor(model => model.descripcion)</td>
                </tr>
                <tr>
                    <td colspan="8"></td>
                    <td>@Html.LabelFor(model => model.sub_total)</td>
                    <td>$@Html.EditorFor(model => model.sub_total)</td>
                    <td>@Html.ValidationMessageFor(model => model.sub_total)</td>
                </tr>
                <tr>
                    <td colspan="8"></td>
                    <td>@Html.LabelFor(model => model.descuento)</td>
                    <td>$@Html.EditorFor(model => model.descuento)</td>
                    <td>@Html.ValidationMessageFor(model => model.descuento)</td>
                </tr>
                <tr>
                    <td colspan="8"></td>
                    <td>@Html.LabelFor(model => model.iva)</td>
                    <td>$@Html.EditorFor(model => model.iva)</td>
                    <td>@Html.ValidationMessageFor(model => model.iva)</td>
                </tr>
                <tr>
                    <td colspan="8"></td>
                    <td>@Html.LabelFor(model => model.total)</td>
                    <td>$@Html.EditorFor(model => model.total)</td>
                    <td>@Html.ValidationMessageFor(model => model.total)</td>
                </tr>
            </table>
        </div>
        <h3>DATO DEL CLIENTE</h3>
        <div>
            <table style="font-size:small;height:auto;">
                <tr>
                    <td hidden="hidden">@Html.LabelFor(model => model.clsPersona.id_persona)</td>
                    <td hidden="hidden">@Html.EditorFor(model => model.clsPersona.id_persona)</td>
                    <td hidden="hidden">@Html.ValidationMessageFor(model => model.clsPersona.id_persona)</td>
                    <td>@Html.LabelFor(model => model.clsPersona.identificacion)</td>
                    <td>@Html.TextBoxFor(model => model.clsPersona.identificacion, new { style = "width:300px" })<input value="..." type="button" onclick="BusquedaClientes()" /></td>
                    <td>@Html.ValidationMessageFor(model => model.clsPersona.identificacion)</td>
                    <td></td>
                    <td>@Html.LabelFor(model => model.clsPersona.nombres_completos)</td>
                    <td>@Html.TextBoxFor(model => model.clsPersona.nombres_completos, new { style = "width:300px" })</td>
                    <td>@Html.ValidationMessageFor(model => model.clsPersona.nombres_completos)</td>
                </tr>
                <tr>
                    <td>@Html.LabelFor(model => model.clsPersona.email)</td>
                    <td>@Html.TextBoxFor(model => model.clsPersona.email, new { style = "width:200px" })</td>
                    <td>@Html.ValidationMessageFor(model => model.clsPersona.email)</td>
                    <td></td>
                    <td>@Html.LabelFor(model => model.clsPersona.telefono)</td>
                    <td>@Html.TextBoxFor(model => model.clsPersona.telefono, new { style = "width:200px" })</td>
                    <td>@Html.ValidationMessageFor(model => model.clsPersona.telefono)</td>
                </tr>
                <tr>
                    <td>@Html.LabelFor(model => model.clsPersona.direccion)</td>
                    <td>@Html.TextBoxFor(model => model.clsPersona.direccion, new { style = "width:300px" })</td>
                    <td>@Html.ValidationMessageFor(model => model.clsPersona.direccion)</td>
                </tr>
            </table>
        </div>
    </div>
 </fieldset>
    
    <div id="frmBusquedaCliente" title="Clientes" hidden="hidden">
        <fieldset>
            <legend></legend>
            <table style="font-size:small">
                <tr>
                    <td>Identificacion</td>
                    <td>@Html.DropDownList("cmbPersonId", new SelectList(Model.lstPersona, "identificacion", "identificacion", "null"))</td>
                </tr>
                <tr>
                    <td>Nombre</td>
                    <td>@Html.DropDownList("cmbPersonNom", new SelectList(Model.lstPersona, "identificacion", "nombres_completos", "null"))</td>
                </tr>
            </table>
        </fieldset>
    </div>
    
    <div id="frmNuevo" title="Nuevo Producto" hidden="hidden">
        <fieldset>
            <legend></legend>
            <table style="font-size:small">
                <tr>
                    <td>Producto</td>
                    <td>@Html.DropDownList("cmbProducto", new SelectList(Model.lstProd, "id_producto", "nombre", "null"))</td>
                </tr>
                <tr>
                    <td>Precio</td>
                    <td><input id="d_precio" type="number" /></td>
                </tr>
            </table>
        </fieldset>
    </div>
    
    <fieldset>
        <legend>Detalle</legend>
            <input id="btnNuevo" type="button" value="Nuevo Producto" onclick="nueva_fila()"/>
        @*<div id="example" class="k-content">*@
        <div id="detalle" class="k-content">
            <div id="grid" style="font-size:small;"></div>
                <script>
        try {
            $("#cod_documento").val('@Model.cod_documento');
            $("#fecha").val('@Model.fecha');
            $("#descripcion").val('@Model.descripcion');
            $("#clsPersona_nombres_completos").val('@Model.clsPersona.nombres_completos');
            $("#clsPersona_identificacion").val('@Model.clsPersona.identificacion');
            $("#clsPersona_email").val('@Model.clsPersona.email');
            $("#clsPersona_telefono").val('@Model.clsPersona.telefono');
            $("#clsPersona_direccion").val('@Model.clsPersona.direccion');
            $("#sub_total").val('@Model.sub_total');
            $("#descuento").val('@Model.descuento');
            $("#iva").val('@Model.iva');
            $("#total").val('@Model.total');

            function pr() {
                var grid = $("#grid").data("kendoGrid");
                grid.addRow();
                grid.saveChanges().delegate(function myfunction() {
                    alert(44);
                });
            }
            @*//var jscombo = '@Model.JScombo';*@
            var jsgrid = '@Model.JSlstDet';
            

        } catch (e) {
        }
                </script>
        </div>
    </fieldset>
        <p>
            <input type="button" value="Guardar" onclick="guarda_cabecera()"/>
        </p>
    
    </div>
}

<script>
    if ('@Model.error' != "") {
        alert('@Model.error');
    }
    function guarda_cabecera()
    {
        var idcab = $("#id_doc_cab").val();
        var coddoc = $("#cod_documento").val();
        var fech = $("#fecha").val();
        var descrip = $("#descripcion").val();
        var idCli = $("#clsPersona_identificacion").val();
        var nomCli = $("#clsPersona_nombres_completos").val();
        
        var email = $("#clsPersona_email").val();
        var tele = $("#clsPersona_telefono").val();
        var direc = $("#clsPersona_direccion").val();

        var subT = $("#sub_total").val().replace(",",".");
        var desc = $("#descuento").val().replace(",", ".");
        var iva = $("#iva").val().replace(",", ".");
        var tot = $("#total").val().replace(",", ".");
        $.ajax({
            url: '@Url.Action("GuardarCab", "Doc")',
            dataType: "html",
            cache: false,
            data: {
                idcabecera: idcab,
                coddocumento: coddoc,
                fecha: fech,
                descripcion: descrip,
                idcliente: idCli,
                nombre: nomCli,
                email: email,
                telefono: tele,
                direccion: direc,
                subtotal: subT,
                descuento: desc,
                iva: iva,
                total: tot
            }
        }).done(function (data2) {
            if (data2) {
                if (data2 > 0) {
                    guarda_detalle(data2);
                } else {
                    alert(data2);
                }
            }
        });
    }

    function guarda_detalle(id_cab) {
        $("#id_doc_cab").val(id_cab);
        var currentDataItem;
        var entityGrid = $("#grid").data("kendoGrid");
        var dato = entityGrid.dataSource.data();
        var count = dato.length;
        for (var i = 0; i < count; i++) {
            currentDataItem = dato[i];

            $.ajax({
                url: '@Url.Action("GuardarDetalle", "Doc")',
                dataType: "html",
                cache: false,
                data: {
                    idCab: id_cab,
                    idDet: i+1,
                    prod: currentDataItem.id_producto,
                    precio: currentDataItem.precio
                }
            }).done(function (data2) {
                if (count==i+1) {
                    alert(data2);
                }
            });
        }
    }

</script>
<script>
    setTimeout('limpiar()', 1000);
    function limpiar() {
        $("#pantalla").removeAttr("hidden");
        $("#btnNuevo").attr("hidden", "hidden");
        var icons = {
            header: "ui-icon-circle-arrow-e",
            activeHeader: "ui-icon-circle-arrow-s"
        };

        $("#downtab").accordion({
            collapsible: true,
            icons: icons
        });

        $(document).ready(function () {
            var dataSource = new kendo.data.DataSource({
                pageSize: 20,
                data: eval(jsgrid),
                //autoSync: true,
                change: function (e) {
                    ActualizaTotal();
                },
                schema: {
                    model: {
                        id: "id_doc_det",
                        fields: {
                            id_doc_cab: { editable: false, nullable: false },
                            id_doc_det: { editable: false, nullable: false },
                            id_producto: { editable: true, type: "number", defaultValue: 1 },
                            precio: { editable: true, type: "number" }
                        }
                    }
                }
            });

            $("#grid").kendoGrid({
                dataSource: dataSource,
                pageable: true,
                height: 200,
                //toolbar: ["create"],
                columns: [
                    //{ field: "id_doc_cab", title: "Cabecera", width: "0px" },
                    //{ field: "id_doc_det", title: "Detalle", width: "0px" },
                    { field: "id_producto", title: "Producto", values: eval(@Html.Raw(Model.JScombo)) },
                        { field: "precio", title: "Precio", format: "{0:c}" },
                        { command: ["edit", "destroy"], title: " ", width: "180px" }],
                    editable: "popup",
                    sortable: {
                        mode: "single",
                        allowUnsort: true
                    }, selectable: "multiple"
                });
        });
    }
</script>
@section Scripts {
    @Scripts.Render("~/bundles/jqueryval")
}

